package com.beiing.tianshuai.tianshuai.dongtai.view;

import android.content.Context;
import android.graphics.Point;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/8/24 0024.
 */

public class DynamicPublishImageAdapter extends BaseRecyclerViewAdapter<String> {

    private Context mContext;
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    DynamicPublishImageAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getItemCount() {
        if (mList != null && mList.size() < 9) {
            return mList.size() + 1;
        } else if (mList != null && mList.size() == 9) {
            return mList.size();
        } else {
            return 1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_publish_image, parent, false);
        holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            Display display = null;
            if (manager != null) {
                display = manager.getDefaultDisplay();
            }
            Point point = new Point();
            if (display != null) {
                display.getSize(point);
            }
            int width = point.x / 4 - (15 * 4) - 20;
            ViewGroup.LayoutParams params = ((ViewHolder) holder).mImage.getLayoutParams();
            params.width = width;
            params.height = width;
            if (mList.size() < 9 && position == mList.size()) {
                ((ViewHolder) holder).mDelete.setVisibility(View.GONE);
            } else {
                ((ViewHolder) holder).mDelete.setVisibility(View.VISIBLE);
            }
            ((ViewHolder) holder).mImage.setLayoutParams(params);
            ((ViewHolder) holder).mImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onImageClickListener(view, holder.getAdapterPosition());
                }
            });
            ((ViewHolder) holder).mDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onImageDeleteClickListener(view, holder.getAdapterPosition());
                }
            });
            if (mList.size() < 9 && position != mList.size()) {
                RequestOptions avatarOptions = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .placeholder(R.drawable.img_loading_background)
                        .centerCrop()
                        .override(width, width);
                Glide.with(mContext.getApplicationContext())
                        .load(TextUtils.isEmpty(
                                mList.get(position)) ?
                                R.drawable.img_loading_background : mList.get(position))
                        .apply(avatarOptions)
                        .into(((ViewHolder) holder).mImage);
            } else if (mList.size() == 9) {
                RequestOptions avatarOptions = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .placeholder(R.drawable.img_loading_background)
                        .centerCrop()
                        .override(width, width);
                Glide.with(mContext.getApplicationContext())
                        .load(TextUtils.isEmpty(
                                mList.get(position)) ?
                                R.drawable.img_loading_background : mList.get(position))
                        .apply(avatarOptions)
                        .into(((ViewHolder) holder).mImage);
            } else if (mList.size() < 9 && position == mList.size()) {
                ((ViewHolder) holder).mImage.setBackground(ContextCompat.getDrawable(mContext, R.drawable.add_pic_border));
                ((ViewHolder) holder).mImage.setImageResource(R.mipmap.add_big);
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_delete)
        ImageView mDelete;
        @BindView(R.id.iv_image)
        ImageView mImage;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void onImageClickListener(View view, int position);

        void onImageDeleteClickListener(View view, int position);
    }
}
